package com.iuie.ns.datum.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.datum.service.IFileInfoService;
import com.iuie.ns.datum.service.IFileService;
import com.iuie.ns.datum.vo.FileInfoVO;


@Controller
@RequestMapping(value="/")
public class FileUploadController {
	
	@Resource
	private IFileService fileService;
	
	@Resource
	private IFileInfoService fileInfoService;
	
	
	@RequestMapping(value="/fileDelete.do",method=RequestMethod.GET)
	public String fileDelete(Long fileId){
		fileService.deleteFileById(fileId);
		return "redirect:fileList.do";
	}
	@RequestMapping(value="/updateDownloadCount.do",method=RequestMethod.GET)
	public @ResponseBody FileInfoVO updateDownloadCount(Long fileId){
		FileInfoVO fileInfo = fileInfoService.findById(fileId);
		return fileInfo;
	}
	
	@RequestMapping(value="/fileDownload.do",method=RequestMethod.GET)
	public ResponseEntity<byte[]> fileDownload(Long fileId,Boolean download,HttpServletRequest request){
		ResponseEntity<byte[]> respResult = null;
		try {
			respResult = fileService.fileDownload(fileId,download);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return respResult;
	}
}

